package com.cloud.bc.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.json.JSONUtil;
import com.cloud.bc.model.entity.BcUsers;
import com.cloud.bc.model.entity.BcWechausers;
import com.cloud.bc.model.enums.CookieConst;
import com.cloud.bc.model.enums.RedisKey;
import com.cloud.bc.service.BcSinglesService;
import com.cloud.bc.service.BcUsersService;
import com.cloud.bc.service.BcWechausersService;
import com.cloud.common.security.AESUtil;
import com.cloud.common.util.CookieUtils;
import com.cloud.redis.config.RedisUtils;
import com.cloud.wx.model.response.AccessTokenResponse;
import com.cloud.wx.model.response.SnsapiUserinfoResponse;
import com.cloud.wx.util.TokenService;
import com.cloud.wx.util.WxCheckSignatureService;
import com.cloud.wx.util.WxUserinfoService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Struct;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@AllArgsConstructor
@Controller
@RequestMapping("wx")
public class WxController extends BaseController {

    private final WxCheckSignatureService wxCheckSignatureService;

    private final BcWechausersService bcWechausersService;
    private final BcUsersService bcUsersService;
    private final BcSinglesService bcSinglesService;

    /**
     * 第一次使用微信，开通接口时使用
     */
    @ResponseBody
    @GetMapping("checkToken")
    public String checkToken(@RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp,
                             @RequestParam("nonce") String nonce, @RequestParam("echostr") String echostr) {
        return wxCheckSignatureService.checkSignature(signature, timestamp, nonce, echostr);
    }

    @SneakyThrows
    @GetMapping("login")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response
    ) {
        Integer id = bcWechausersService.getOpenid(request, response);
        if (id <= 0) {
            //未获取到openid
            return new ModelAndView("redirect:/m");
        }

        //1.判断当前用户是否已经绑定openid
        int countUser = bcUsersService.count(new BcUsers().setOpenid(id));
        if (countUser > 0) {
            //说明已经绑定了
            //查询出cookie信息，进行绑定
            List<BcUsers> bcUsersList = bcUsersService.selectAll(new BcUsers().setOpenid(id));
            BcUsers bcUsersResult = bcUsersList.stream().findFirst().orElse(new BcUsers());
            bcUsersService.login(request, response, bcUsersResult);
            //如果已经绑定则直接跳转到首页
            return new ModelAndView("redirect:/m/center");
        }
        //如果未绑定跳转到绑定页,这里只传openid的关系
        String aesId = AESUtil.encrypt(Convert.toStr(id), config.getOpenidSecret());
        return new ModelAndView("redirect:/m/login?openid=" + aesId);
    }

    //提现微信绑定
    @GetMapping("withdrawalBind")
    public ModelAndView withdrawalBind(HttpServletRequest request, HttpServletResponse response) {
        Integer id =  bcWechausersService.getOpenid(request, response);
        if (id <= 0) {
            //未获取到openid
            return new ModelAndView("redirect:/m");
        }
        String openidEncode = AESUtil.encrypt(Convert.toStr(id), config.getOpenidSecret());
        return new ModelAndView("redirect:/m/center/account/withdrawal/wxpay/edit?openid="+openidEncode);
    }
}
